package com.fr.lintcode;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月23日
*时间：下午4:23:13
*/
public class Q118 {
    /**
     * @param S, T: Two string.
     * @return: Count the number of distinct subsequences
     */
    public int numDistinct(String S, String T) {
        // write your code here
        if (S.length() < T.length()) {
            return 0;
        }
        if (T.length() == 0)
            return 1;
        int dp[][] = new int[S.length() + 1][T.length() + 1];
        dp[0][0] = 1;
        for (int i = 0; i < dp.length; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= S.length(); i++) {
            for (int j = 1; j <= T.length(); j++) {
                dp[i][j] = dp[i - 1][j];
                if (S.charAt(i - 1) == T.charAt(j - 1)) {
                    dp[i][j] += dp[i - 1][j - 1];
                }
            }
        }
        return dp[S.length()][T.length()];
    }

    @Test
    public void testA() {
        System.out.println(numDistinct("rabbbit", "rabbit"));
    }

}
